<?php

/**
	前台显示礼券及兑换处理
	
	@author xiaoming<maixiaoming@9qi.cn>
*/
class CouponAction extends BaseAction 
{
	
    /**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	/**
		优惠券首页，显示当前优惠券信息列表
	*/
	public function index() 
	{
		$this->_checkLogin();
		$Coupon = M('coupon');
		$catId = isset($_POST['coupon_cat']) ? intval($_POST['coupon_cat']) : 0;
		//echo $catId;die;
		import("ORG.Util.Page"); // 导入分页类
		
		$userPoints = $this->$userPoints;
		
		$condition = "is_show=1  ";
		if(!empty($catId)) $condition .= " AND cat=$catId" ;
		
		$count = $Coupon->where($condition)->count();
		$Page = new Page($count,6);
		$show = $Page->show(); // 分页显示输出
		
		$result = $Coupon->where($condition)
						 ->limit($Page->firstRow.','.$Page->listRows)->select();
		
		if ($result) {
			foreach ($result as &$c) {
				$c['url'] = U("Apps://coupon/view?id={$c['c_id']}");
			}
		}
		
		//输出分类列表
		$Coupon_cat = M('coupon_cat');
		$coupon_cats = $Coupon_cat->select(); 
		$this->assign('coupon_cats',$coupon_cats);
		$this->assign('catId',$catId);
		
		$this->assign('title','Coupon_list');
		$this->assign('coupons',$result);
		$this->assign('page',$show);
		$this->display();
	}
	
	/**
		查看该优惠券详细信息
	*/
	public function view()
	{
		$this->_checkLogin();  //检查登录
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if ($id) {
			$Coupon = M('coupon');
			$result = $Coupon->where("c_id=$id")->find();
			if ($result) {
				$this->assign('coupon',$result);
				$this->assign('u_id',$this->userId);
				//$this->assign('cid',$id);
				$this->display();
			} else {
				echo "<script>alert('coupon not found');</script>";
				$this->redirect('index');
			}
		} else {
			$this->redirect('index');
		}
	}
	
	/**
		會員兌換優惠券頁面
	*/
	public function exchange() 
	{
		$this->_checkLogin();  //检查登录
		$cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		$Coupon = M('coupon');
		$result = $Coupon->where("c_id=$cid")->find();
		if ($result) {
			$this->assign('coupon',$result);
			$this->assign('title','兌換優惠券');
			$this->display();
		} else {
			//無此優惠券
			$this->redirect('index', array(), 3, '無此優惠券！');
		}
	}
	
	/**
		會員兌換優惠券處理程序
	*/
	public function action() 
	{
		$this->_checkLogin();  //检查登录
		$cid = isset($_POST['c_id']) ? intval($_POST['c_id']) : 0;
		
		$userPoints = $this->userPoints;
		$Coupon = M('coupon');
		$result = $Coupon->where("c_id=$cid")->find();
		if ($result) {
			//當前會員積分與優惠券積分比較
			if ($userPoints >= $result['points']){
				
				//把訂單信息插入數據庫
				$Order = M('order');
				$Order->create();
				$Order->u_id = $this->userId;
				$Order->points = $result['points'];
				$Order->order_time = time();
				
				
				//插入成功，扣除該會員積分
				if($Order->add()) {
					$User = M('user');
					
					$data = array();
					$data['u_id'] = $this->userId;
					$data['u_points'] = $userPoints - $result['points'];

					if($User->save($data)) {
						$this->redirect('index', array(), 3, '提交成功！');
					}
				}
				
			} else {
				//會員積分不足
				$this->redirect('index', array(), 3, '積分不足！');
			}
		} else {
			//無此優惠券
			$this->redirect('index', array(), 3, '無此優惠券！');
		}
		
	}
}